/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.access.RequestParameter;
import com.elf.gwt.common.client.model.CacheModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.InlineLabel;

/**
 *  
 */
public class CacheComponent extends JSONServerComponent
{	
	protected CacheModel cacheModel = null;
	protected int selectedGroupId = 0;
	protected String action = "";
	protected HorizontalPanel buttonPanel = null;
	protected Button cleanButton;
	protected Button cleanImagesButton;
	protected Button warmButton;
	
	/**
	 * Constructor
	 */
	public CacheComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		setupTimer(900000);
	}

	public boolean updateFromHistoryParameters(String[] params)
	{
		if (params.length > 1) selectedGroupId = Integer.parseInt(params[1]);

		return super.updateFromHistoryParameters(params);
	}

	public void additionalRequestParameters()
	{
		addRequestParameter(new RequestParameter("action", action));
		action = "";
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		cacheModel = new CacheModel(jsonObject);
	}

	public void onBuilt()
	{
		setVisible(!controller.isInitializing());
	}

	public void buildFromModel() throws Exception
	{
		clear();
		updateButtonPanel();
		add(buttonPanel);
		add(new InlineHTML("<br/><br/><br/>"));
		add(new InlineLabel("Number of Component Cache files: " + cacheModel.getCount()));
		add(new InlineHTML("<br/><br/>"));
		add(new InlineLabel("Number of Image Cache files: " + cacheModel.getFileCount()));
		
		onBuilt();
	}

	protected ClickHandler cleanHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			action = "Clean";
			build(true);
		}
	};

	protected ClickHandler cleanImagesHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			action = "CleanImages";
			build(true);
		}
	};

	protected ClickHandler warmHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			action = "Warm";
			build(true);
		}
	};

	public void updateButtonPanel() {
		if (buttonPanel == null)
		{
			buttonPanel = new HorizontalPanel();
			buttonPanel.setStyleName("ZButtons");

			cleanButton = new Button("clean cache files", cleanHandler);
			cleanButton.addStyleName("ComponentLink");
			buttonPanel.add(cleanButton);
			buttonPanel.add(new InlineHTML("&nbsp;&nbsp;"));

			cleanImagesButton = new Button("clean cache images", cleanImagesHandler);
			cleanImagesButton.addStyleName("ComponentLink");
			buttonPanel.add(cleanImagesButton);
			buttonPanel.add(new InlineHTML("&nbsp;&nbsp;"));

			warmButton = new Button("warm cache files", warmHandler);
			warmButton.addStyleName("ComponentLink");
			buttonPanel.add(warmButton);
			buttonPanel.add(new InlineHTML("&nbsp;&nbsp;"));
		}
	}
}
